package com.sino.second.struts2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.sino.business.service.LoginInfo;
import com.sino.model.BasicInfoModel;
import com.sino.model.ExpertModel;
import com.sino.second.business.IExpertsVoteService;
import com.sino.second.business.IReviewService;
import com.sino.second.model.ExpertsReviewModel3;
import com.sino.second.model.VoteModel;
import com.sino.second.model.XmReviewResults2;
import com.sino.second.model.XmReviewResults3;
import com.sino.util.Config;
import com.sino.util.SysParameter;

public class ExpertsVoteAction extends ActionSupport {
	private IExpertsVoteService expertsVoteServiceImpl;// 声明IExpertsVoteService业务类

	private List<XmReviewResults2> XmReviewResults2List = null;// 存储通过复审的项目信息

	private List<VoteModel> voteList = null;// 记录投票计算结果，用于投票与投票结果的展示与存储

	private List<ExpertModel> emList;// 列出被分配到委员会的所有专家

	private String expertId;// 记录专家id，用于修改投票

	private List<ExpertsReviewModel3> expertsReviewModel3List = null;// 存储大评委分数表数据，用于展示投票结果修改与查看

	private int assignExpertCount;// 被分配到委员会的专家数

	private int alreadyVoteExpertCount;// 已经投票的专家数

	private List<XmReviewResults3> xmReviewResults3List = null;// 存储评委会结果表记录，用于展示最终投票结果

	private boolean voteFlag = true;// 用于判断专家投票是否关闭，true表示可以投票，false表示投票关闭

	private String fileName;// 下载文件名

	private InputStream fileInputStream;// 下载输入流

	/**
	 * @param expertsVoteServiceImpl
	 *            the expertsVoteServiceImpl to set
	 */
	public void setExpertsVoteServiceImpl(
			IExpertsVoteService expertsVoteServiceImpl) {
		this.expertsVoteServiceImpl = expertsVoteServiceImpl;
	}

	/**
	 * @return the xmReviewResults2List
	 */
	public List<XmReviewResults2> getXmReviewResults2List() {
		return XmReviewResults2List;
	}

	/**
	 * @param xmReviewResults2List
	 *            the xmReviewResults2List to set
	 */
	public void setXmReviewResults2List(
			List<XmReviewResults2> xmReviewResults2List) {
		XmReviewResults2List = xmReviewResults2List;
	}

	/**
	 * @return the voteList
	 */
	public List<VoteModel> getVoteList() {
		return voteList;
	}

	/**
	 * @param voteList
	 *            the voteList to set
	 */
	public void setVoteList(List<VoteModel> voteList) {
		this.voteList = voteList;
	}

	/**
	 * @return the emList
	 */
	public List<ExpertModel> getEmList() {
		return emList;
	}

	/**
	 * @param emList
	 *            the emList to set
	 */
	public void setEmList(List<ExpertModel> emList) {
		this.emList = emList;
	}

	/**
	 * @return the expertId
	 */
	public String getExpertId() {
		return expertId;
	}

	/**
	 * @param expertId
	 *            the expertId to set
	 */
	public void setExpertId(String expertId) {
		this.expertId = expertId;
	}

	/**
	 * @return the expertsReviewModel3List
	 */
	public List<ExpertsReviewModel3> getExpertsReviewModel3List() {
		return expertsReviewModel3List;
	}

	/**
	 * @param expertsReviewModel3List
	 *            the expertsReviewModel3List to set
	 */
	public void setExpertsReviewModel3List(
			List<ExpertsReviewModel3> expertsReviewModel3List) {
		this.expertsReviewModel3List = expertsReviewModel3List;
	}

	/**
	 * @return the assignExpertCount
	 */
	public int getAssignExpertCount() {
		return assignExpertCount;
	}

	/**
	 * @param assignExpertCount
	 *            the assignExpertCount to set
	 */
	public void setAssignExpertCount(int assignExpertCount) {
		this.assignExpertCount = assignExpertCount;
	}

	/**
	 * @return the alreadyVoteExpertCount
	 */
	public int getAlreadyVoteExpertCount() {
		return alreadyVoteExpertCount;
	}

	/**
	 * @param alreadyVoteExpertCount
	 *            the alreadyVoteExpertCount to set
	 */
	public void setAlreadyVoteExpertCount(int alreadyVoteExpertCount) {
		this.alreadyVoteExpertCount = alreadyVoteExpertCount;
	}

	/**
	 * @return the xmReviewResults3List
	 */
	public List<XmReviewResults3> getXmReviewResults3List() {
		return xmReviewResults3List;
	}

	/**
	 * @param xmReviewResults3List
	 *            the xmReviewResults3List to set
	 */
	public void setXmReviewResults3List(
			List<XmReviewResults3> xmReviewResults3List) {
		this.xmReviewResults3List = xmReviewResults3List;
	}

	/**
	 * @return the voteFlag
	 */
	public boolean isVoteFlag() {
		return voteFlag;
	}

	/**
	 * @param voteFlag
	 *            the voteFlag to set
	 */
	public void setVoteFlag(boolean voteFlag) {
		this.voteFlag = voteFlag;
	}

	/**
	 * @return the fileInputStream
	 */
	public InputStream getFileInputStream() {
		return fileInputStream;
	}

	/**
	 * @param fileInputStream
	 *            the fileInputStream to set
	 */
	public void setFileInputStream(InputStream fileInputStream) {
		this.fileInputStream = fileInputStream;
	}

	/**
	 * @return the fileName
	 */
	public String getFileName() {
		return fileName;
	}

	/**
	 * @param fileName
	 *            the fileName to set
	 */
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	/**
	 * 查询列出被分配到大评委组的专家，并查询大评委结果表，如果大评委结果表存在数据，则将专家投票关闭
	 * 
	 * @return
	 */
	public String listThirdReviewExpert() {
		String hqlWhere = " tzpf.year=" + SysParameter.CURRENT_YEAR;// 获得系统年度
		String hqlWhere2 = " xmjg3.year=" + SysParameter.CURRENT_YEAR;// 获得系统年度
		emList = this.expertsVoteServiceImpl.listReviewExpertByXmpsmc(
				SysParameter.THIRDREVIEW, hqlWhere);// 调用expertsVoteServiceImpl的方法读取专家分配表中大评委组的专家
		xmReviewResults3List = this.expertsVoteServiceImpl
				.listAllXmReviewResults3(hqlWhere2);// 调用expertsVoteServiceImpl的方法读取大评委结果表中的数据
		if (null == xmReviewResults3List || xmReviewResults3List.size() < 1) {
			voteFlag = true;// 判断如果大评委结果表中不存在数据，则将专家投票开启
		} else {
			voteFlag = false;// 否则将专家投票关闭
		}
		return SUCCESS;
	}

	/**
	 * 查询复审结果表中的项目信息，用于委员会专家投票
	 * 
	 * @return
	 */
	public String listAllPastSecondReviewProject() {
		voteList = null;// 将投票结果list置空，防止缓存影响投票结果
		String hqlWhere = " tx2.year=" + SysParameter.CURRENT_YEAR;// 获得系统年度
		XmReviewResults2List = this.expertsVoteServiceImpl
				.listAllPastSecondReviewProject(hqlWhere);// 调用expertsVoteServiceImpl的方法读取复审表中的项目信息
		return SUCCESS;
	}

	/**
	 * 查询大评委分数表中的项目信息，用于专家修改投票结果
	 * 
	 * @return
	 */
	public String listAlreadyAttendThirdReviewProject() {
		voteList = null;// 将投票结果list置空，防止缓存影响投票结果
		String hqlWhere = " tx3.year=" + SysParameter.CURRENT_YEAR;// 获得系统年度
		expertsReviewModel3List = this.expertsVoteServiceImpl
				.listThirdReviewProjectByExpert(expertId, hqlWhere);// 调用expertsVoteServiceImpl的方法读取大评委分数表中的数据，用于修改专家投票
		return SUCCESS;
	}

	/**
	 * 读取大评委分数表中的项目信息，用于查询专家投票结果
	 * 
	 * @return
	 */
	public String viewAlreadyAttendThirdReviewProject() {
		String hqlWhere = " tx3.year=" + SysParameter.CURRENT_YEAR;// 获得系统年度
		expertsReviewModel3List = this.expertsVoteServiceImpl
				.listThirdReviewProjectByExpert(expertId, hqlWhere);// 调用expertsVoteServiceImpl的方法读取大评委分数表中的数据，用于查询专家投票
		return SUCCESS;
	}

	/**
	 * 读取大评委分数表，并经过一定的计算，展示专家投票计算结果信息
	 * 
	 * @return
	 */
	public String listVoteResult() {
		String hqlWhere = " xmfs3.year=" + SysParameter.CURRENT_YEAR;// 获得系统年度
		String hqlWhere2 = " tzpf.year=" + SysParameter.CURRENT_YEAR;// 获得系统年度
		String hqlWhere3 = " tx3.year=" + SysParameter.CURRENT_YEAR;// 获得系统年度
		voteList = this.expertsVoteServiceImpl.listTblXmfs3VoteResult(hqlWhere);// 调用expertsVoteServiceImpl的方法，读取大评委分数表，并经过一定的计算，展示专家投票计算结果信息
		assignExpertCount = this.expertsVoteServiceImpl
				.listAssignExpertCountByXmpsmc(SysParameter.THIRDREVIEW,
						hqlWhere2);// 调用expertsVoteServiceImpl中的方法，读取专家分配表，查询已经被分配到大评委的专家数
		alreadyVoteExpertCount = this.expertsVoteServiceImpl
				.listAlreadyVoteExpertCount(hqlWhere3);// 调用expertsVoteServiceImpl的方法，读取大评委分数表，查询已经进行投票的专家数
		return SUCCESS;
	}

	/**
	 * 读取大评委分数表，并经过一定的计算，按照评选等级进行排序展示专家投票结果信息
	 * 
	 * @return
	 */
	public String listVoteResultSort() {
		String hqlWhere = " xmfs3.year=" + SysParameter.CURRENT_YEAR;// 获得系统年度
		String hqlWhere2 = " tzpf.year=" + SysParameter.CURRENT_YEAR;// 获得系统年度
		String hqlWhere3 = " tx3.year=" + SysParameter.CURRENT_YEAR;// 获得系统年度
		voteList = this.expertsVoteServiceImpl
				.listTblXmfs3VoteResultSort(hqlWhere);// 调用expertsVoteServiceImpl的方法，读取大评委分数表，并经过一定的计算，按照评选等级进行排序展示专家投票计算结果信息
		assignExpertCount = this.expertsVoteServiceImpl
				.listAssignExpertCountByXmpsmc(SysParameter.THIRDREVIEW,
						hqlWhere2);// 调用expertsVoteServiceImpl中的方法，读取专家分配表，查询已经被分配到大评委的专家数
		alreadyVoteExpertCount = this.expertsVoteServiceImpl
				.listAlreadyVoteExpertCount(hqlWhere3);// 调用expertsVoteServiceImpl的方法，读取大评委分数表，查询已经进行投票的专家数
		return SUCCESS;
	}

	/**
	 * 增加或者修改大评委分数表中的一条项目记录
	 * 
	 * @return
	 */
	public String addProjectVote() {
		String hqlWhere = " tx3.year=" + SysParameter.CURRENT_YEAR;// 获得系统年度
		if (null != voteList && voteList.size() > 0) {// 判断投票结果list是否为空
			for (VoteModel vm : voteList) {

				ExpertsReviewModel3 expertsReviewModel3 = new ExpertsReviewModel3();// 新建一个大评委分数model

				BasicInfoModel basicInfoModel = new BasicInfoModel();
				basicInfoModel.setZlid(vm.getBasicInfoId());
				expertsReviewModel3.setBasicInfoModel(basicInfoModel);// 存入项目model

				ExpertModel expertModel = new ExpertModel();
				expertModel.setFBh(new Integer(expertId));
				expertsReviewModel3.setExpertModel(expertModel);// 存入专家model

				expertsReviewModel3.setPsrq(new Date());// 存入当前日期时间

				expertsReviewModel3.setYear(SysParameter.CURRENT_YEAR);// 存入当前年份

				this.expertsVoteServiceImpl.saveOrUpdateExpertsReviewModel3(
						expertsReviewModel3, vm.getJydjId(), vm.getPxdjId(),
						hqlWhere);// 调用expertsVoteServiceImpl的方法增加或者更新大评委分数表中的一条项目信息
			}
		}
		return SUCCESS;
	}

	/**
	 * 将投票中间计算结果生成最终结果，并插入到大评委结果表中
	 * 
	 * @return
	 */
	public String createFinalVoteResult() {
		String hqlWhere = " xmjg3.year=" + SysParameter.CURRENT_YEAR;// 获得系统年度
		this.expertsVoteServiceImpl.delTblXmjg3(hqlWhere);// 清空大评委结果表
		if (null != voteList && voteList.size() > 0) {// 判断投票结果list是否为空
			for (VoteModel vm : voteList) {
//				System.out.println("fdfdsfasf" + vm.getToThirdCheck());
				if (1 == vm.getToThirdCheck()) {
					XmReviewResults3 xmReviewResults3 = new XmReviewResults3();// 新建一个大评委结果表model

					BasicInfoModel basicInfoModel = new BasicInfoModel();
					basicInfoModel.setZlid(vm.getBasicInfoId());

					xmReviewResults3.setBasicInfoModel(basicInfoModel);// 存入项目信息

					xmReviewResults3
							.setPs(new Integer(vm.getFinalPrizeCount()));// 存入票数信息

					xmReviewResults3.setPsrq(new Date());// 存入当前日期时间

					xmReviewResults3.setYear(SysParameter.CURRENT_YEAR);// 存入当前年份

					String username = ((LoginInfo) (ServletActionContext
							.getRequest().getSession()
							.getAttribute("logininfo"))).getName();// 通过session获得系统登录用户

					xmReviewResults3.setUsername(username);// 存入当前操作用户

					this.expertsVoteServiceImpl.saveOrUpdateXmReviewResults3(
							xmReviewResults3, vm.getJydjId(), vm
									.getFinalPrizeId(), hqlWhere);// 调用expertsVoteServiceImpl的方法增加或者更新一条大评委结果表中项目信息
				}
			}
		}
		/**
		 * 以下代码为关闭当前页面
		 */
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=utf-8");
		try {
			PrintWriter out = response.getWriter();
			out
					.println("<script language='JavaScript' type='text/JavaScript'>");
			out.println("alert('生成结果成功，请到结果查看页面进行查看！');"); 
			out.println("window.location.href('listVoteResult.action');");
			out.println("</script>");
		} catch (IOException e) {
			throw new RuntimeException("生成投票结果异常！");
		}

		return null;
	}

	/**
	 * 查询大评委结果表，列出最终投票结果
	 * 
	 * @return
	 */
	public String listFinalVoteResult() {
		String hqlWhere = " xmjg3.year=" + SysParameter.CURRENT_YEAR;// 获得系统年度
		xmReviewResults3List = this.expertsVoteServiceImpl
				.listAllXmReviewResults3(hqlWhere);// 调用expertsVoteServiceImpl的方法，查询大评委结果表中的数据，用于展示最终投票结果
		return SUCCESS;
	}

	/**
	 * 清空大评委结果表数据
	 * 
	 * @return
	 */
	public String delTblXmjg3() {
		String hqlWhere = " xmjg3.year=" + SysParameter.CURRENT_YEAR;// 获得系统年度
		this.expertsVoteServiceImpl.delTblXmjg3(hqlWhere);
		/**
		 * 以下重新加载当前页面
		 */
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=utf-8");
		try {
			PrintWriter out = response.getWriter();
			out
					.println("<script language='JavaScript' type='text/JavaScript'>");
			out.println("alert('删除成功,请进入查询投票计算结果重新生成！');");
			out.println("window.location.href('listFinalVoteResult.action');");
			out.println("</script>");
		} catch (IOException e) {
			throw new RuntimeException("删除异常！");
		}

		return null;
	}

	public String exportTblXmjg3() {
		String hqlWhere = " xmjg3.year=" + SysParameter.CURRENT_YEAR;// 获得系统年度
		String path = Config.findHomeDir();// 当前文件所在的根目录
		String fName="voteResult";
		try {
			fName=new String("大评委投票结果列表".getBytes("GBK"),"ISO8859_1");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		String fileName = path + "\\tepFile3\\"+fName+".xls";
		xmReviewResults3List = this.expertsVoteServiceImpl
				.listAllXmReviewResults3(hqlWhere);// 调用expertsVoteServiceImpl的方法，查询大评委结果表中的数据，用于导出最终投票结果
		File exportFile = this.expertsVoteServiceImpl.exportVoteResult(
				fileName, xmReviewResults3List);
		if (null != exportFile) {
			this.setFileName(exportFile.getName());
			InputStream inputStream;
			try {
				inputStream = new FileInputStream(exportFile);
				this.setFileInputStream(inputStream);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		return SUCCESS;
	}
}
